<?
		require_once('../inc/Home.php');
        session_start();
        if(!isset($_SESSION['cart']))
                $_SESSION['cart'] = array();
        $cart = $_SESSION['cart'];
        $requestMethod = isset($_REQUEST['_method']) ? $_REQUEST['_method'] : $_SERVER['REQUEST_METHOD'];
		
        switch ($requestMethod) {
                case 'POST':
                        if(isset($cart[$_GET['id']]))
                        {
                                $item = $cart[$_GET['id']]['count'] += $_REQUEST['count'];
								$row = GetItemDetail($_GET['id']);
								$pricebyamount = $cart[$_GET['id']]['price'] += $row['Price'];
								$total = $cart[$_GET['id']]['amount'] += $row['Price'];
                        }else{
								$row = GetItemDetail($_GET['id']);							   
                                $cart[$_GET['id']] = array('id'=> $_GET['id'], 'count'=> $_REQUEST['count'], 'price'=> $row['Price'], 'amount' => $row['Price'] * $_REQUEST['count'] ); 
								                       
                        }
                        
                        echo json_encode($cart[$_GET['id']]);                                                           
                        break;
                case 'PUT':
                        $cart[$_GET['id']] = array('id'=> $_GET['id'], 'count'=> $_REQUEST['count'], 'price'=> $_REQUEST['Price'], 'amount' => $_REQUEST['amount']);
                        echo json_encode($cart[$_GET['id']]);                                                           
                        break;
                case 'DELETE':
                                echo 'success';                 
                        break;
                default:
                        if(empty($_GET['id']))
                        {
                                echo json_encode($cart);                                
                        }else{
                                echo json_encode($cart[$_GET['id']]);                                                           
                        }
                        break;
        }
        $_SESSION['cart'] = $cart;